function Map() {
	this.obj = {};
	this.count = 0;
}
Map.prototype.put = function(key, value) {
	var oldValue = this.obj[key];
	if(oldValue == undefined) {
		this.count++;
	}
	this.obj[key] = value;
}
Map.prototype.get = function(key) {
	return this.obj[key];
}
Map.prototype.remove = function(key) {
	var oldValue = this.obj[key];
	if(oldValue != undefined) {
		this.count--;
		delete this.obj[key];
	}
}
Map.prototype.size = function() {
	return this.count;
}

var language = {
	datas: [],
	text: function(key){
		
		if(language.datas.length == 0){
			var _shopInfo = cacheSessionStorage.getCache('shopInfo');
			if(_shopInfo && _shopInfo.languageBase){
				language.datas = JSON.parse(cacheSessionStorage.getCache('shopInfo').languageBase);
			}
		}
		
		var defaultValue = '请检查设置!';
		
		$$.each(language.datas, function(index, _data){
			if(_data.key == key) {
				defaultValue = _data.value;
				return false;
			}
		});
		return defaultValue;
	},
	render: function(){
		var _languagedoms = $$('.language');
		$$.each(_languagedoms, function(index, _languagedom){
			$$(this).prepend(language.text($$(this).attr('language')));
			$$(this).removeClass('language');
		})
		
		var _languageplaceholderdoms = $$('.language-placeholder');
		$$.each(_languageplaceholderdoms, function(index, _languageplaceholderdom){
			$$(this).attr('placeholder', language.text($$(this).attr('language')));
			$$(this).removeClass('language-placeholder');
		})
	},
}

var commonConfig = {
	urlMap: null,
	mgUrlMap: null,
	hostCustomerMap: null,
	fbPiexIdMap: null, 
	gaTrackingIdMap: null,
	
	initUrlMap: function() {
		commonConfig.urlMap = new Map();
		commonConfig.urlMap.put('127.0.0.1' , 'http://localhost:9020');
		commonConfig.urlMap.put('47.236.181.95' , 'http://47.236.181.95:9020');
		commonConfig.urlMap.put('www.buyfunshop.com' , 'https://fr.buyfunshop.com');
	},
	websitePrefix: function() {
		if(commonConfig.urlMap == null) commonConfig.initUrlMap();
        return commonConfig.urlMap.get(window.location.hostname) || 'https://fr.buyfunshop.com';
	},
	initHostCustomerMap: function() {
		commonConfig.hostCustomerMap = new Map();
        commonConfig.hostCustomerMap.put('www.buyfunshop.com', '111652591152902');
	},
	getCustomerIdByHost: function() {
		if(commonConfig.hostCustomerMap == null) commonConfig.initHostCustomerMap();
		return commonConfig.hostCustomerMap.get(window.location.hostname) || '111652591152902';
	},
	
	
	initFbPiexIdMap:function(){
		commonConfig.fbPiexIdMap = new Map();
	},
	getFbPiexId: function() {
		var _piexId = cacheSessionStorage.getCache('piexId');
		if(_piexId && _piexId != '') return _piexId ; 
		if(commonConfig.fbPiexIdMap == null)commonConfig.initFbPiexIdMap();
        return commonConfig.fbPiexIdMap.get(window.location.hostname) || '1186670511849514';
	},
	
	initGATracking: function(){
		commonConfig.gaTrackingIdMap = new Map();
        commonConfig.gaTrackingIdMap.put('www.buyfunshop.com' , 'UA-174903169-1');
	},
	getGATracking : function(){
		if(commonConfig.gaTrackingIdMap == null)commonConfig.initGATracking();
		return commonConfig.gaTrackingIdMap.get(window.location.hostname) || 'UA-174903169-1';
	}
}

var tools = {
	initFbPiexId: function(){
		var _piexId = tools.getURLParam('piexId');
		if(_piexId && _piexId != '') cacheSessionStorage.setCache('piexId' , _piexId);
	},
	initAffiliate: function(){
		var _affiliate = tools.getURLParam('affiliate');
		if(_affiliate && _affiliate != '') cacheCookie.setCache('affiliate' , _affiliate, 28);
	},
	initToTop: function() {
		$$('<div class="tools-toTop" button class="mdui-btn mdui-btn-icon"><svg t="1587019424038" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7050" width="24" height="24"><path d="M500.8 461.909333L267.306667 695.296l-45.226667-45.269333 278.741333-278.613334L779.306667 650.026667l-45.248 45.226666z" p-id="7051"></path></svg></div>').appendTo('body');
		if((document.body.scrollTop || document.documentElement.scrollTop) == 0) {
			$$(".tools-toTop").hide();
		}
		window.onscroll = function(event) {
			if(document.body.scrollTop <= 480 && document.documentElement.scrollTop <= 480) {
				$$(".tools-toTop").hide();
			} else {
				$$(".tools-toTop").show();
			}
		}
		$$(".tools-toTop").off('click').on('click', function(e) {
			document.body.scrollTop = 0;
			document.documentElement.scrollTop = 0;
			_monitor.addMonitor({'eventAims':'TO_TOP', 'eventName': 'CLICK', 'aimsCategory': 'EVENT'});
		});
	},
	initCustmerService: function() {
		if($$('meta[name="customer-service"]').length > 0){
			$$('<a id="customer_service" href="https://m.me/'+ commonConfig.getCustomerIdByHost() +'"><div class="tools-customer" button class="mdui-btn mdui-btn-icon"><svg t="1594291858511" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1145" width="24" height="24"><path d="M512 102.4c-226.23232 0-409.6 168.06912-409.6 375.47008 0 81.33632 28.52864 156.4672 76.57472 217.99936l-73.97376 178.54464a34.12992 34.12992 0 1 0 44.60544 44.5952l217.8048-90.20416a440.75008 440.75008 0 0 0 144.59904 24.53504c226.27328 0 409.6-168.20224 409.6-375.47008C921.6 270.46912 738.27328 102.4 512 102.4z m0 682.65984c-41.3696 0-82.50368-7.00416-122.2656-20.79744l-24.50432-8.45824-165.632 68.59776 57.67168-139.20256-24.50432-31.34464c-40.64256-52.06016-62.09536-112.92672-62.09536-176.00512 0-169.39008 153.09824-307.2 341.32992-307.2 188.20096 0 341.32992 137.79968 341.32992 307.2 0 169.34912-153.12896 307.21024-341.32992 307.21024z" p-id="1146" fill="#000000"></path><path d="M722.66752 423.7312l-48.27136-48.26112-108.26752 108.22656-108.25728-108.22656-156.53888 156.52864 48.30208 48.26112 108.2368-108.25728 108.25728 108.25728z" p-id="1147" fill="#000000"></path></svg></path></svg></div></a>').appendTo('body');
			$$('#customer_service').on('click', function(e){
				_monitor.addMonitor({'eventAims':'CUSTOM_SERVICE', 'eventName': 'CLICK', 'aimsCategory': 'EVENT'});
			});
		}
	},
	initPromotionTip: function() {
		if($$('meta[name="promotion-tip"]').length > 0){
			$$('<div id="mdui-nav-orders" class="mdui-nav-orders"></div>').appendTo('body');
			setInterval(function(){
				var _index = Math.floor(Math.random()*24) ;
				var _dom = $$("#mdui-nav-orders");
				var arr = ['趙','錢','孫','李','上官','周','吳','王','馮','程','姜','於','馬','楊','曾','陳','林','沈','賴','薛','劉','江','胡','蔡','郭','朱','秦','許','呂','何','張','淩','包','車'];
				_dom.html(`最新訂單來自${arr[_index]}**${_index}秒前`);
				_dom.css('display' , _dom.css('display') == 'none' ? 'block' : 'none');
			}, 3000);
		}
	},
	initAjaxSetup: function() {
		$$.ajaxSetup({
			headers: {
				requestId: $$.guid(),
				requestTime: (new Date()).valueOf(),
				requestHost: window.location.host
			},
			dataType: 'json',
			complete: function(){
				tools.hideLoading();
			}
		});
	},
	getURLParam: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r != null) return unescape(r[2]);
		return '';
	},
	loading: function(zindex) {
		$$.showOverlay(zindex || 6999);
		$$('<div class="loader"></div>').appendTo('body');
	},
	hideLoading: function() {
		$$('body .loader').remove();
		$$.hideOverlay();
	},
	loadCssCode: function(code,pageCustomVal){
		var style = document.createElement('style');
		style.type = 'text/css' , style.rel = 'stylesheet' , style.pageCustom = pageCustomVal;
		try{
			style.appendChild(document.createTextNode(code));
		}catch(ex){
			style.styleSheet.cssText = code;
		}
		var head = document.getElementsByTagName('head')[0];
		head.appendChild(style);
	},
	removeTemplateCssCode: function(){
		var styles = $$('style');
		$$.each(styles , function(index ,style){
			if(style.pageCustom && style.pageCustom == 'pageCustom'){
				style.remove();
			}
		});
	},
	loadScriptAsync: function(src, callback) {
		var script = document.createElement('script'),
			head = document.getElementsByTagName('head')[0];
		script.type = 'text/javascript';
		script.charset = 'UTF-8';
		script.src = src;
		if(script.addEventListener) {
			script.addEventListener('load', function() {
				callback();
			}, false);
		} else if(script.attachEvent) {
			script.attachEvent('onreadystatechange', function() {
				var target = window.event.srcElement;
				if(target.readyState == 'loaded') {
					callback();
				}
			});
		}
		head.appendChild(script);
	},
	getShopCartCount: function(){
		var _count = 0 ;
		var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ;
		$$.each(cartProducts, function(index, temp){
			_count += parseInt(temp.quantity);
		});
		return _count;
	},
	initShopInfo: function(){
		var shopInfo = cacheSessionStorage.getCache('shopInfo');
		if(!shopInfo){
			$$.ajax({
				async: false,
				url : commonConfig.websitePrefix() + '/api/shop/name',
				success : function(result){
					if(result.code == 1){
						shopInfo = result.datas ;
						cacheSessionStorage.setCache('shopInfo' , shopInfo);
					}
				},
				error: function(xhr, textStatus) {
					document.title = "" ; 
				}
			});
		}
		
		document.title = shopInfo.domainName;
		tools.loadCssCode('.price > .salePrice:before, .salePrice:before{content: "'+ shopInfo.currencySymbol +'"}');
		tools.loadCssCode('.price > .originalPrice:before, .originalPrice:before{content: "'+ shopInfo.currencySymbol +'"}');
	}
}

var _marketing = {
	renderPromotions: function(){
	},
	openMarketing: function(){
	},
	closeMarketing: function(){
		$$('#marketing_dialog').hide()
		$$.hideOverlay();
		$$('body').css({'overflow': 'auto'})
	}
}

var _drawer = {
	inst : null,
	open: function(){
		
		if(_drawer.inst == null)_drawer.init();
		
		_drawer.inst.open();
	},
	init: function(){
		
		_drawer.inst = new mdui.Drawer('#left-drawer');
		
		var drawer = document.getElementById('left-drawer');
		
		drawer.addEventListener('close.mdui.drawer', function () {
			$$('.mdui-bottom-nav a').removeClass('mdui-bottom-nav-active');
			$$('.mdui-bottom-nav a').eq(0).addClass('mdui-bottom-nav-active');
		});
	},
	renderDrawer: function(navs){
		
	}
	
}

var _monitor = {
    addMonitor: function(data, event) {
		var body = document.getElementsByTagName('body')[0];
		var log_pic = document.createElement('img');
		if(!data.eventAims || data.eventAims == ''){
			data['eventAims'] = encodeURIComponent(location.href);
		}
		log_pic.src = commonConfig.websitePrefix() + '/api/monitor/ut' + _monitor.createParams(data);
		body.appendChild(log_pic);
		body.removeChild(log_pic);
	},
	createParams:function(json){
        var parameStr = '?';
        for(var i in json){
			if(typeof json[i] == 'object'){
				parameStr += i + '=' + encodeURI(JSON.stringify(json[i])) + '&'; 
			}else{
				parameStr += i + '=' + encodeURI(json[i]) + '&'; 
			} 
        }
        parameStr = parameStr.substring(0,parameStr.length - 1);
        return parameStr;
	},
	pageMonitor: function(){
		_monitor.addMonitor({'aimsCategory': 'PAGE','eventName': 'OPEN'});
	}
};
var cacheCookie = {
	setCache: function(name, value, days) {
		var Days = days || 30;
		var exp = new Date();
		exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
		document.cookie = name + "=" + escape(JSON.stringify(value)) + '; path=/' + "; expires=" + exp.toGMTString(); //直接设置
	},
	getCache: function(name) {
		var arr = document.cookie.split('; '); //多个cookie值是以; 分隔的，用split把cookie分割开并赋值给数组
		if(arr && arr.length > 0){
			for(var i = 0; i < arr.length; i++) {
				var arr2 = arr[i].split('='); //原来割好的数组是：user=simon，再用split('=')分割成：user simon 这样可以通过arr2[0] arr2[1]来分别获取user和simon 
				if(arr2 && arr2[0] && arr2[0] == name) { //如果数组的属性名等于传进来的name
					return JSON.parse(unescape(arr2[1])); //就返回属性名对应的值
				}
			}
		}
		return null; //没找到就返回空
	},
	clearCookie: function() {
		var _hasClear = cacheCookie.getCache('hasClearCookie');
		if(_hasClear && _hasClear != '') {
			return;
		}
		var rs = document.cookie.match(new RegExp("([^ ;][^;]*)(?=(=[^;]*)(;|$))", "gi"));
		// 删除所有cookie  
		for(var i in rs) {
			document.cookie = rs[i] + "=;expires=Mon, 26 Jul 1997 05:00:00 GMT; path=/; ";
		}
		cacheCookie.setCache('hasClearCookie', 'true', 90);
	}
}
var cacheLocalstorage = {
	setCache: function(name, value, days) {
		if(days <= 0) localStorage.removeItem(name);

		localStorage.setItem(name, escape(JSON.stringify(value)));
	},
	getCache: function(name) {
		var _value = localStorage.getItem(name);
		if(_value && _value != '') {
			return JSON.parse(unescape(_value));
		}
		return null;
	},
	removeCache : function(name){
		localStorage.removeItem(name);
	},
	canUse: function(){
		if(cacheLocalstorage.getCache('test')){
			return true;
		}
		try {
			cacheLocalstorage.setCache('test', 'testValue')
			return true;
		} catch (error) {
			return false;
		}
	}
}
var cacheSessionStorage = {
	setCache: function(name, value) {
		sessionStorage.setItem(name, escape(JSON.stringify(value)));
	},
	getCache: function(name) {
		var _value = sessionStorage.getItem(name);
		if(_value && _value != '') {
			return JSON.parse(unescape(_value));
		}
		return null;
	},
	removeCache : function(name){
		sessionStorage.removeItem(name);
	},
	canUse: function(){
		if(cacheSessionStorage.getCache('test')){
			return true;
		}
		try {
			cacheSessionStorage.setCache('test', 'testValue')
			return true;
		} catch (error) {
			return false;
		}
	}
}

var footprint = {
	data: {
		inst : null,
		contentHtml: `<div class="customer-cart-dialog" id="customer-footprint-dialog">
						<div class="mdui-dialog-title">${language.text("wo_de_zu_ji")}<button class="mdui-btn mdui-btn-icon mdui-ripple mdui-btn-delete" onclick="footprint.close();"><svg style="margin-bottom: 4px;" t="1587020841649" class="icon" viewBox="0 0 1045 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6538" width="20" height="20"><path d="M282.517333 213.376l-45.354666 45.162667L489.472 512 237.162667 765.461333l45.354666 45.162667L534.613333 557.354667l252.096 253.269333 45.354667-45.162667-252.288-253.44 252.288-253.482666-45.354667-45.162667L534.613333 466.624l-252.096-253.226667z" p-id="6539" fill="#8a8a8a"></path></svg></button></div>
						<div class="mdui-dialog-content" style="height: 100% !important;">
							<div class="template-module-product-list-horizontal">
								<div class="flex-row">
									<ul>
									<% for (var i = 0; i < aggregateModuleProducts.length; i += 2) { %>
										<li>
											<a href="../item/details.html?id=<%=aggregateModuleProducts[i].id%>">
												<div class="image"><img src="<%=aggregateModuleProducts[i].image%>" /></div>
												<div class="infos">
													<div class="name"><%=aggregateModuleProducts[i].title%></div>
													<div class='salesVolume'>ขาย<%= $imports.buyCount(aggregateModuleProducts[i].id)%></div>
													<div class="price">
														<span class="salePrice"><%=aggregateModuleProducts[i].price%></span>
														<span class="originalPrice"><%=aggregateModuleProducts[i].originalPrice%></span>
														<svg t="1595555200274" class="icon" style="position: absolute; right: 0px; top: -4px; width: 24px;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="15723"
														 width="32" height="32">
															<path d="M512 910.2336c219.9552 0 398.2336-178.2784 398.2336-398.2336S731.9552 113.7664 512 113.7664C292.0448 113.7664 113.7664 292.0448 113.7664 512S292.0448 910.2336 512 910.2336z m0 56.832C260.608 967.0656 56.9344 763.392 56.9344 512S260.608 56.9344 512 56.9344s455.0656 203.776 455.0656 455.0656S763.392 967.0656 512 967.0656z"
															 fill="#9F0000" p-id="15724"></path>
															<path d="M341.7088 512h348.0576l36.6592-113.7664H341.7088V512z m0-170.7008h423.7312c15.6672 0 28.4672 12.6976 28.4672 28.4672 0 2.9696-0.512 5.9392-1.3312 8.704l-54.8864 170.7008c-3.7888 11.776-14.7456 19.7632-27.0336 19.7632H341.7088v57.0368h369.5616c15.6672 0 28.4672 12.6976 28.4672 28.4672s-12.6976 28.4672-28.4672 28.4672H341.7088c-31.4368 0-56.9344-25.4976-56.9344-56.9344v-57.0368h-0.4096V341.2992h-28.0576c-15.6672 0-28.4672-12.6976-28.4672-28.4672s12.6976-28.4672 28.4672-28.4672h28.4672c31.4368 0.1024 56.9344 25.6 56.9344 56.9344z"
															 fill="#9F0000" p-id="15725"></path>
															<path d="M341.2992 739.5328m-28.4672 0a28.4672 28.4672 0 1 0 56.9344 0 28.4672 28.4672 0 1 0-56.9344 0Z" fill="#9F0000"
															 p-id="15726"></path>
															<path d="M682.7008 739.5328m-28.4672 0a28.4672 28.4672 0 1 0 56.9344 0 28.4672 28.4672 0 1 0-56.9344 0Z" fill="#9F0000"
															 p-id="15727"></path>
														</svg>
													</div>
												</div>
											</a>
										</li>
									<% } %>
									</ul>
								</div>
								<div class="flex-row">
									<ul>
									<% for (var i = 1; i < aggregateModuleProducts.length; i += 2) { %>
										<li>
											<a href="../item/details.html?id=<%=aggregateModuleProducts[i].id%>">
												<div class="image"><img src="<%=aggregateModuleProducts[i].image%>" /></div>
												<div class="infos">
													<div class="name"><%=aggregateModuleProducts[i].title%></div>
													<div class='salesVolume'>ขาย<%= $imports.buyCount(aggregateModuleProducts[i].id)%></div>
													<div class="price">
														<span class="salePrice"><%=aggregateModuleProducts[i].price%></span>
														<span class="originalPrice"><%=aggregateModuleProducts[i].originalPrice%></span>
														<svg t="1595555200274" class="icon" style="position: absolute; right: 0px; top: -4px; width: 24px;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="15723"
														 width="32" height="32">
															<path d="M512 910.2336c219.9552 0 398.2336-178.2784 398.2336-398.2336S731.9552 113.7664 512 113.7664C292.0448 113.7664 113.7664 292.0448 113.7664 512S292.0448 910.2336 512 910.2336z m0 56.832C260.608 967.0656 56.9344 763.392 56.9344 512S260.608 56.9344 512 56.9344s455.0656 203.776 455.0656 455.0656S763.392 967.0656 512 967.0656z"
															 fill="#9F0000" p-id="15724"></path>
															<path d="M341.7088 512h348.0576l36.6592-113.7664H341.7088V512z m0-170.7008h423.7312c15.6672 0 28.4672 12.6976 28.4672 28.4672 0 2.9696-0.512 5.9392-1.3312 8.704l-54.8864 170.7008c-3.7888 11.776-14.7456 19.7632-27.0336 19.7632H341.7088v57.0368h369.5616c15.6672 0 28.4672 12.6976 28.4672 28.4672s-12.6976 28.4672-28.4672 28.4672H341.7088c-31.4368 0-56.9344-25.4976-56.9344-56.9344v-57.0368h-0.4096V341.2992h-28.0576c-15.6672 0-28.4672-12.6976-28.4672-28.4672s12.6976-28.4672 28.4672-28.4672h28.4672c31.4368 0.1024 56.9344 25.6 56.9344 56.9344z"
															 fill="#9F0000" p-id="15725"></path>
															<path d="M341.2992 739.5328m-28.4672 0a28.4672 28.4672 0 1 0 56.9344 0 28.4672 28.4672 0 1 0-56.9344 0Z" fill="#9F0000"
															 p-id="15726"></path>
															<path d="M682.7008 739.5328m-28.4672 0a28.4672 28.4672 0 1 0 56.9344 0 28.4672 28.4672 0 1 0-56.9344 0Z" fill="#9F0000"
															 p-id="15727"></path>
														</svg>
													</div>
												</div>
											</a>
										</li>
									<% } %>
									</ul>
								</div>
							</div>
						</div>
					</div>`
	},
	open: function(){
		var footMark = cacheLocalstorage.getCache('footMark');
		if(!footMark || footMark.length == 0){
			mdui.snackbar({message: language.text('ni_hai_mei_lai_guo_ne') + '^_^',position: 'top',timeout:1500});
		}else{
			footprint.render(footMark.reverse());
		}
	},
	render: function(footMark){
		if(footprint.data.inst == null){
			$$('body').append(template.render(footprint.data.contentHtml, {'aggregateModuleProducts': footMark}));
			footprint.data.inst = $$('#customer-footprint-dialog');
		}
		footprint.data.inst.show();
		document.body.style.overflow = 'hidden';
		_monitor.addMonitor({'eventAims':'FOOT_PRINT', 'eventName': 'CLICK', 'aimsCategory': 'EVENT'});
	},
	close: function(){
		footprint.data.inst.hide();
		document.body.style.overflow = 'auto';
	}
}

var customerCart = {
	data: {
		inst : null,
		basicHtml: `<div class="customer-cart-dialog" id="customer-cart-dialog">
						<div class="mdui-dialog-title">${language.text("gou_wu_che")}<button class="mdui-btn mdui-btn-icon mdui-ripple mdui-btn-delete" onclick="customerCart.closeCart();"><svg style="margin-bottom: 4px;" t="1587020841649" class="icon" viewBox="0 0 1045 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6538" width="20" height="20"><path d="M282.517333 213.376l-45.354666 45.162667L489.472 512 237.162667 765.461333l45.354666 45.162667L534.613333 557.354667l252.096 253.269333 45.354667-45.162667-252.288-253.44 252.288-253.482666-45.354667-45.162667L534.613333 466.624l-252.096-253.226667z" p-id="6539" fill="#8a8a8a"></path></svg></button></div>
						<div class="mdui-dialog-content"></div>
						<div class="mdui-dialog-actions"></div>
					</div>`,
		normalHtml: {
			content:   `<div class="customer-cart-promotion">
							<span><%=basicPriceInfo%></span><%=promotionInfo%>
						</div>
						<div class="customer-cart-lists">
							<ul>
								<% for (var i = 0; i < caculateInfos.cartProductDTOs.length; i++) { %>
								<li class="list-item-product" _productId="<%=caculateInfos.cartProductDTOs[i].productId%>" _productSkuId="<%=caculateInfos.cartProductDTOs[i].productSkuId%>" _productSkuBarcode="<%=caculateInfos.cartProductDTOs[i].productSkuBarcode%>" _quantity="<%=caculateInfos.cartProductDTOs[i].quantity%>">
									<button class="mdui-btn mdui-btn-icon radios <%= caculateInfos.cartProductDTOs[i].checked ? 'active' : '' %>"><i class="mdui-icon material-icons"><%= caculateInfos.cartProductDTOs[i].checked ? 'radio_button_checked' : 'radio_button_unchecked' %></i></button>
									<a href="../item/details.html?id=<%=caculateInfos.cartProductDTOs[i].productId%>">
										<img class="product-img" src="<%=caculateInfos.cartProductDTOs[i].productImageUrl%>" />
									</a>
									<div class="product-text">
										<span class="title"><strong>限时抢购</strong><%=caculateInfos.cartProductDTOs[i].productTitle%></span>
										<span class="skuName"><%=caculateInfos.cartProductDTOs[i].productSkuName%></span>
										<div class="price">
											<span class="salePrice"><%=caculateInfos.cartProductDTOs[i].productPrice%></span>
											<span class="originalPrice"><%=caculateInfos.cartProductDTOs[i].originalProductPrice%></span>
										</div>
									</div>
									<div class="mdui-numbox-container">
										<div class="mdui-numbox" data-numbox-step='1' data-numbox-min='1' data-numbox-max='999'>
											<button class="mdui-numbox-btn mdui-numbox-btn-minus"><i class="mdui-icon material-icons">remove</i></button>
											<input class="mdui-numbox-input" type="number" value="<%=caculateInfos.cartProductDTOs[i].quantity%>" onfocus="this.blur()" readonly />
											<button class="mdui-numbox-btn mdui-numbox-btn-plus"><i class="mdui-icon material-icons">add</i></button>
										</div>
									</div>
									<button class="mdui-btn mdui-btn-icon mdui-ripple mdui-btn-delete">
										<svg t="1587091164561" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7482" width="20" height="20"><path d="M341.013333 394.666667l27.754667 393.450666h271.829333l27.733334-393.450666h64.106666L704.426667 792.618667a64 64 0 0 1-63.829334 59.498666H368.768a64 64 0 0 1-63.829333-59.52L276.885333 394.666667h64.128z m139.306667 19.818666v298.666667h-64v-298.666667h64z m117.013333 0v298.666667h-64v-298.666667h64zM181.333333 288h640v64h-640v-64z m453.482667-106.666667v64h-256v-64h256z" p-id="7483" fill="#8a8a8a"></path></svg>
									</button>
								</li>
								<% } %>
							</ul>
						</div>`,
			actions:   `<div class="mdui-dialog-actions-checkAll">
							<button class="mdui-btn mdui-btn-icon radios <%= active %>">
								<i class="mdui-icon material-icons"><%= active == 'active' ? 'radio_button_checked' : 'radio_button_unchecked' %></i>
							</button>
							<span>${language.text("xuan_ze")}</span>
						</div>
						<div class="customer-cart-price">
							<div class="top">
								<span>${language.text("he_ji")}:</span>
								<span>$<%= caculateInfos.price %></span>
							</div>
							<% if (caculateInfos.fullReductionPromotion && caculateInfos.fullReductionPromotion.reductionValue) { %>
							<div class="bottom">${language.text("you_hui")} <%=caculateInfos.fullReductionPromotion.reductionValue%></div>
							<% } %>
						</div>
		     			<button onclick="customerCart.toCheckOut(1)" class="mdui-btn mdui-ripple cod">${language.text("hdfk")}</button>`
		},
		emptyHtml :{
			content:  	`<div class="customer-cart-empty">
							<img src="../resources/img/empty_cart.png" />
							<span>${language.text("gou_wu_che_wei_kong")}</span>
							<a href="../item/list.html" class="mdui-btn">${language.text("qu_guangguang")}</a>
						</div>` ,
			actions: 	``
		}
	},

	openCart: function(){
		if(customerCart.data.inst == null){
			$$('body').append(customerCart.data.basicHtml);
			customerCart.data.inst = $$('#customer-cart-dialog');
		}
		customerCart.data.inst.show();
		document.body.style.overflow = 'hidden';
		
		var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ;
		if(cartProducts.length == 0){
			customerCart.openEmptyCart();
		}else{
			customerCart.openNormalCart();
		}
	},
	closeCart: function(){
		//location.reload();
		customerCart.data.inst.hide();
		document.body.style.overflow = 'auto';
	},
	openEmptyCart: function(){
		$$('#customer-cart-dialog .mdui-dialog-content').html(customerCart.data.emptyHtml.content);
		$$('#customer-cart-dialog .mdui-dialog-actions').css('background' , '#F0F0F0');
		$$('#customer-cart-dialog .mdui-dialog-actions').html(customerCart.data.emptyHtml.actions);
	},
	openNormalCart: function(){
		var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ; 
		$$.each(cartProducts, function(index, temp){
			temp.checked = true;
		});
		cacheLocalstorage.setCache('cartProducts', cartProducts);
		
		var normalInfos = {caculateInfos: customerCart.getCaculateInfos() , estimatedTime: customerCart.getEstimatedTime(), basicPriceInfo: customerCart.getBasicPriceInfo, promotionInfo: customerCart.getPromotion(), active: 'active', totals: 0};
		
		var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ;
		$$.each(cartProducts , function(index , cartProduct){
			normalInfos.active = cartProduct.checked ? 'active' : '';
			normalInfos.totals += cartProduct.checked ? 1 : 0 ;
		});
		
		$$('#customer-cart-dialog .mdui-dialog-content').html(template.render(customerCart.data.normalHtml.content, normalInfos));
		$$('#customer-cart-dialog .mdui-dialog-actions').css('background' , '#FFFFFF');
		$$('#customer-cart-dialog .mdui-dialog-actions').html(template.render(customerCart.data.normalHtml.actions, normalInfos));
		
		customerCart.addNormalCartEvents();
	},
	getCaculateInfos: function(){
		tools.loading();
		var _caculateInfos = {};
		$$.ajax({
			url : commonConfig.websitePrefix() + '/api/shop/cart/calculation',
			method: 'POST',
			async: false,
			contentType : "application/json ; charset=utf-8",
		  	data: JSON.stringify({cartProductRequests: cacheLocalstorage.getCache('cartProducts') || []}),
			success : function(result){
				if(result.code == 1){
					cacheLocalstorage.setCache('cartProducts' , result.datas.cartProductDTOs);
					_caculateInfos = result.datas;
				}else{
					window.location.href = '../item/list.html' ;
				}
			},
			error: function(xhr, textStatus) {
				if(xhr.status == 500 && JSON.parse(xhr.response) && JSON.parse(xhr.response).body && JSON.parse(xhr.response).body.message){
					app.methods.goHome();
				}
			},complete: function(){tools.hideLoading();}
		});
		return _caculateInfos ;
	},
	getEstimatedTime: function(){
		var _startDate = new Date() ; 
		var _endDate = new Date() ; 
		_startDate.setDate(_startDate.getDate() + 7);
		_endDate.setDate(_endDate.getDate() + 10);
		return _startDate.Format('yyyy-MM-dd') + '~' + _endDate.Format('yyyy-MM-dd') ;
	},
	getBasicPriceInfo: function(){
		return language.text('qcm')+ cacheSessionStorage.getCache('shopInfo').basicPrice + language.text('by') ;
	},
	getPromotion: function(){
		var _fullReduction = cacheSessionStorage.getCache('shopInfo').fullReduction;
		var _html = '' ;
		if(_fullReduction && _fullReduction.length > 0){
			$$.each(_fullReduction, function(index, tmp) {
				_html += language.text('qcm') + tmp.fullValue+ language.text('reduce') + tmp.reductionValue + (index != _fullReduction.length - 1 ? '，' : '。')
			});
		}
		return _html ; 
	},
	addNormalCartEvents: function(){
		customerCart.deleteProductEvents();
		customerCart.triggerCheckEvents();
		customerCart.changeQuantityEvents();
	},
	deleteProductEvents: function(){
		$$('.customer-cart-lists > ul > li >button.mdui-btn-delete').off().on('click' , function(){
			var _that = this;
			var _cartProducts = cacheLocalstorage.getCache('cartProducts') || [];
    		var _productDOM = $$(_that).parent();
    		
    		var _deleteIndex = -1 ; 
    		$$.each(_cartProducts, function (index, _cartProduct) {
    			if(_cartProduct.productId == _productDOM.attr('_productid') && _cartProduct.productSkuId == _productDOM.attr('_productskuid')){
    				_deleteIndex = index;
    				return false;
    			}
			});
			if(_deleteIndex >= 0){
				_monitor.addMonitor({'eventAims': _productDOM.attr('_productid'), 'eventName': 'CART_DELETE', 'aimsCategory': 'PRODUCT'});
				_cartProducts.splice(_deleteIndex , 1);
				cacheLocalstorage.setCache('cartProducts' , _cartProducts);
        		customerCart.openCart();
        		
        		mdui.snackbar({message: language.text('shan_chu_cheng_gong'),position: 'bottom',timeout:500});
			} 
		})
	},
	triggerCheckEvents: function(){
		$$('.customer-cart-lists > ul > li > button.radios').off('click').on('click' , function(e){
			if($$(this).hasClass('active')){
				$$(this).removeClass('active');
				$$(this).html('<i class="mdui-icon material-icons">radio_button_unchecked</i>');
			}else{
				$$(this).addClass('active');
				$$(this).html('<i class="mdui-icon material-icons">radio_button_checked</i>');
			}
			
			var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ;
			$$.each(cartProducts , function(index , cartProduct){
				//找到當前DOM
				cartProduct.checked = customerCart.findProductDOM(cartProduct);
			});
			cacheLocalstorage.setCache('cartProducts' , cartProducts);
			customerCart.openCart();
			
		});
		
		$$('.customer-cart-dialog .mdui-dialog-actions .mdui-dialog-actions-checkAll > button.mdui-btn').off('click').on('click' , function(e){
			var _tempCheck = $$(this).hasClass('active') ;
			if($$(this).hasClass('active')){
				$$(this).removeClass('active');
				$$(this).html('<i class="mdui-icon material-icons">radio_button_unchecked</i>');
			}else{
				$$(this).addClass('active');
				$$(this).html('<i class="mdui-icon material-icons">radio_button_checked</i>');
			}
			var cartProducts = cacheLocalstorage.getCache('cartProducts') || [] ;
			$$.each(cartProducts , function(index , cartProduct){
				//找到當前DOM
				cartProduct.checked = !_tempCheck;
			});
			cacheLocalstorage.setCache('cartProducts' , cartProducts);
			customerCart.openCart();
		});
	},
	changeQuantityEvents: function(){
		$$('.customer-cart-lists .mdui-numbox > .mdui-numbox-btn.mdui-numbox-btn-minus').off().on('click' , function(e){
			var _input = $$(this).siblings('.mdui-numbox-input');
			var _quantity =  parseInt(_input.val()) - parseInt($$(this).parent().attr('data-numbox-step'));
			_quantity = (_quantity <= 0) ? 1 : _quantity;
			if(parseInt(_input.val()) != _quantity){
				_input.val(_quantity);
				
				var _cartProducts = cacheLocalstorage.getCache('cartProducts') || [];
				var _productDOM = $$(this).parent().parent().parent();
				$$.each(_cartProducts, function (index, _cartProduct) {
	    			if(_cartProduct.productId == _productDOM.attr('_productid') && _cartProduct.productSkuId == _productDOM.attr('_productskuid')){
	    				_cartProduct.quantity = _quantity ; 
	    			}
				});
				cacheLocalstorage.setCache('cartProducts' , _cartProducts);
				customerCart.openCart();
			}
			
		});
		$$('.customer-cart-lists .mdui-numbox > .mdui-numbox-btn.mdui-numbox-btn-plus').off().on('click' , function(e){
			var _input = $$(this).siblings('.mdui-numbox-input');
			var _quantity =  parseInt(_input.val()) + parseInt($$(this).parent().attr('data-numbox-step'));
			_quantity = (_quantity <= 0) ? 1 : _quantity;
			if(parseInt(_input.val()) != _quantity){
				_input.val(_quantity);
				
				var _cartProducts = cacheLocalstorage.getCache('cartProducts') || [];
				var _productDOM = $$(this).parent().parent().parent();
				$$.each(_cartProducts, function (index, _cartProduct) {
	    			if(_cartProduct.productId == _productDOM.attr('_productid') && _cartProduct.productSkuId == _productDOM.attr('_productskuid')){
	    				_cartProduct.quantity = _quantity ; 
	    			}
				});
				cacheLocalstorage.setCache('cartProducts' , _cartProducts);
				customerCart.openCart();
			}
		});
	},
	findProductDOM: function(cartProduct){
		var _liDoms = $$('.customer-cart-lists > ul > li');
		var _radiosDOM = null;
		$$.each(_liDoms ,function(index , lidom){
			var _liDom = $$(lidom);
		    if(_liDom.attr('_productid') == cartProduct.productId && _liDom.attr('_productskuid') == cartProduct.productSkuId){
		    	_radiosDOM = _liDom.children('.mdui-btn.mdui-btn-icon.radios') ; 
		    	return false;
		    }
		});
		return _radiosDOM != null ? _radiosDOM.hasClass('active') : false;
	},
	toCheckOut: function(checkType){
		cacheLocalstorage.removeCache('buynowCartProducts');
		var _hasChecked = false;
		var _cartProducts = cacheLocalstorage.getCache('cartProducts') || [];
		$$.each(_cartProducts, function (index, _cartProduct) {
			if(_cartProduct.checked){
				_hasChecked = true ;  
			}
		});
		if(_hasChecked){
			window.location.href = '../cart/index.html?checkType=' + checkType;
		}else{
			mdui.snackbar({message: language.text('wei_xuan_ze_shang_pin') ,position: 'bottom',timeout:800});
		}
	}
}

var globalization = {
	render: function(){
		language.render();
	}
}

$$(function(){
	tools.initAjaxSetup();
	tools.initShopInfo();
	tools.initToTop();
	tools.initFbPiexId();
	tools.initAffiliate();
	tools.initCustmerService();
	//tools.initPromotionTip();
	_monitor.pageMonitor();
	language.render();
});

Date.prototype.Format = function(fmt){
    var o = {   
        "M+" : this.getMonth()+1,                 //月份   
        "d+" : this.getDate(),                    //日   
        "h+" : this.getHours(),                   //小时   
        "m+" : this.getMinutes(),                 //分   
        "s+" : this.getSeconds(),                 //秒   
        "q+" : Math.floor((this.getMonth()+3)/3), //季度   
        "S"  : this.getMilliseconds()             //毫秒   
    };
    if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}